package com;


import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class demo {
// 解释：一个房间有自己的房间号，里面有一堆人，每个人都有面子
    public static void main(String[] args) {

        final List<Room> roomList = initRoom();
        //map方法
        ArrayList<String> list = new ArrayList<>();
        roomList.stream().forEach(room -> {
            room.getPeopleList().forEach(people -> {
                list.add(people.getName());
                System.out.println(list);
            });
        });


        //flatMap方法
        roomList.stream()
                .map(Room::getPeopleList)
                .flatMap(Collection::stream)
                .map(Room.People::getName)
                .collect(Collectors.toList());

    }

    // 初始化10条数据
    private static List<Room> initRoom() {
        return IntStream.range(1, 11)
                .mapToObj(num -> new Room(num, Arrays.asList(new com.demo.Room.People("name:" + num), new Room.People("name:" + 10 * num))))
                .collect(Collectors.toList());
    }


    // 解释：一个房间有自己的房间号，里面有一堆人，每个人都有面子
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    static class Room {
        private int number;
        private List<People> peopleList;


        @Data
        @NoArgsConstructor
        @AllArgsConstructor
        static class People {
            private String name;
        }
    }
}
